package com.neusoft.acquistion.sensor.consumer.task;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.Resource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import java.util.Map;

//@Component
public class ReadSensor {

    @Resource
    private RestTemplate restTemplate;

    @Resource
    WebClient webClient;


//    @Scheduled(cron = "*/5 * * * * ?")
//    @Scheduled除过cron还有三种方式：fixedRate，fixedDelay，initialDelay
    public void sayWord() throws JsonProcessingException {
        Mono<String> mono = webClient
                .get() // GET 请求
                .uri("/monitor/all")  // 请求路径
                .retrieve() // 获取响应体
                .bodyToMono(String.class); //响应数据类型转换
        System.out.println(mono.block());
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, Object> monitorMap = objectMapper.readValue(mono.block().toString(), new TypeReference<Map>() {});
        System.out.println(monitorMap.getClass());
        for (Map.Entry<String, Object> entry : monitorMap.entrySet()) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }
    }

//
//    @Scheduled(cron = "*/5 * * * * ?")
////    @Scheduled除过cron还有三种方式：fixedRate，fixedDelay，initialDelay
//    public void sayWord() {
////        final String url ="https://apis.tianapi.com/toutiaohot/index?key=c65cff44e82fc3b1d27622e80e0a1b6c";
//        final String url = "https://www.baidu.com123/";
//        String result = restTemplate.getForObject(url, String.class);
//        System.out.println(result);
//    }
}
